/*
 *  table_aux.cpp
 *  table_aux
 *
 *  Created by Jaisiel Santana Almeida on 25/11/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#include "table_aux.h"
//#include <hash_map> //cambio a map
#include <map>
#include <string>
#include <iostream>
using namespace std;



bool simbols_table::exist(char * name)
{
	if (table.find(name)!=table.end())
		return true;
	return false;
	
	
};

int simbols_table::insert ( char * name, values val, int typ)
{
	if (this->exist(name))
		return 0;
	node_table node_aux;
	strcpy(node_aux.ID, name);//copy name
	node_aux.TYPE=typ;//save type
	//use switch for decide wich field of union use
	switch (typ)
	{
		case FLOAT:
			node_aux.VALUE.FLOAT_=val.FLOAT_;
			break;
		case INT:
			node_aux.VALUE.INT_=val.INT_;
			break;
		case CHAR:
			node_aux.VALUE.CHAR_=val.CHAR_;
			break;
		case STR:
			strcpy(node_aux.VALUE.STRING,val.STRING);
			break;
		default:
			return -1;
	}
	
	//we've saved node info, it's time to insert it in the hash
	
	//table.insert(table.end(), node_aux); have dudes, must decide use [] operator;
	
	table[name]=node_aux;
	
	return 1;
	
};

node_table simbols_table::get_struct (char *name)
{
	if (this->exist(name))
	{
		return table[name];
	}
	else
	{
		node_table aux;
		return aux;
	}
}


values simbols_table::value (char * name) //return a value of the field value, in case of error return NULL, ¿better trhow an exception?, string coulb be NULL
{
	if(!(this->exist(name)))
		throw "Identifier doesn't exist";
//return the value structure instead of the specific value, pending 
	switch (table[name].TYPE)
	{
		case FLOAT:
			//return table[name].VALUE.FLOAT_;
			break;
		case INT:
			//return table[name].VALUE.INT_;
			break;
		case CHAR:
			//return table[name].VALUE.CHAR_;
			break;
		case STR:
			//char *str= new char[255];
			
			//strcpy(str,table[name].VALUE.STRING);
			//return str;
			break;
		default:
			throw "Identifier doesn't exist";
	}
	return table[name].VALUE; //return all
	
};

int simbols_table::type (char * name)
{
	if(this->exist(name))
		return table[name].TYPE;
	return -1;
	
};


int simbols_table::update (char * name, values val, int typ)  //return 1 if ok, 0 if  no exist or -1 in case error
{
	if (!(this->exist(name) ) )
		return 0;
	switch (typ) //esto primero por si hay error
	{
		case FLOAT:
			table[name].VALUE.FLOAT_=val.FLOAT_;
			break;
		case INT:
			table[name].VALUE.INT_=val.INT_;
			break;
		case CHAR:
			table[name].VALUE.CHAR_=val.CHAR_;
			break;
		case STR:
			strcpy(table[name].VALUE.STRING,val.STRING);
			break;
		default:
			return -1;
	}
	
	table[name].TYPE=typ;
	
	return 1;
	
	
};

void simbols_table::show_table()
{
	for( map<string, node_table>::iterator i=table.begin(); i != table.end(); i++)
	{
		cout << endl <<"Identificador: " << (i->second).ID << "  ";
		switch((i->second).TYPE)
		{
			case FLOAT:
				cout << "TIPO FLOAT, VALOR: " << (i->second).VALUE.FLOAT_;
				break;
			case INT:
				cout << "TIPO INT, VALOR: " << (i->second).VALUE.INT_;
				break;
			case CHAR:
				cout << "TIPO char, VALOR: " << (i->second).VALUE.CHAR_;
				break;
			case STR:
				cout << "TIPO STR, VALOR: " << (i->second).VALUE.STRING;
				break;
			default:
				//return -1;
				;
		}
	}
		cout << endl;
	
};




/* this is an example of use of hash_map structure*/
/*int main1()
{
  hash_map<const char*, int, hash<const char*>, eqstr> months;

  months["january"] = 31;
  months["february"] = 28;
  months["march"] = 31;
  months["april"] = 30;
  months["may"] = 31;
  months["june"] = 30;
  months["july"] = 31;
  months["august"] = 31;
  months["september"] = 30;
  months["october"] = 31;
  months["november"] = 30;
  months["december"] = 31;

  cout << "september -> " << months["september"] << endl;
  cout << "april     -> " << months["april"] << endl;
  cout << "june      -> " << months["june"] << endl;
  cout << "november  -> " << months["november"] << endl;
}*/

